<script setup lang="ts">
import { getCodeAPI, postLoginAPI } from '@/apis/comcom'
import { useUserStore } from '@/stores/user'
import { ref } from 'vue'

defineOptions({
  options: {
    styleIsolation: 'shared',
  },
})

// 是否显示倒计时
const showCountDown = ref(false)
// 获取验证码
const handleGetCode = async () => {
  try {
    await formRef.value?.validateField?.('mobile')
    const data = await getCodeAPI({ mobile: formData.value.mobile, type: 'login' })
    uni.showToast({
      title: '验证码发送成功',
      icon: 'success',
      mask: true,
    })
    console.log('data', data)
    formData.value.code = data.data.code.toString()
    showCountDown.value = true
  } catch {}
}
// 倒计时结束
const handleTimeup = () => {
  console.log('倒计时结束')
  showCountDown.value = false
}

// 1. 表单数据
const formData = ref({
  mobile: '13230000035',
  code: '',
})

// 2. 表单验证规则
const formRules = ref({
  mobile: {
    rules: [
      { required: true, errorMessage: '请输入手机号' },
      { pattern: /^1[3-9]\d{9}$/, errorMessage: '请输入正确的手机号' },
    ],
  },
  code: {
    rules: [
      { required: true, errorMessage: '请输入验证码' },
      { pattern: /^\d{6}$/, errorMessage: '验证码格式错误' },
    ],
  },
})

// 3. 点击登录校验整个表单
const formRef = ref<UniHelper.UniFormsInstance>()
const handleLogin = async () => {
  try {
    await formRef.value?.validate?.()
    if (!isAgree.value) return uni.showToast({ title: '请勾选同意用户协议', icon: 'none' })
    const res = await postLoginAPI(formData.value)
    console.log(res)
    const userStore = useUserStore()
    userStore.setToken(res.data.token)
    userStore.setUserInfo(res.data)
    uni.showToast({ title: '登录成功', icon: 'none' })
    uni.switchTab({ url: '/pages/index/index' })
  } catch (err) {
    return uni.showToast({ title: '请填写完整信息', icon: 'none' })
  }
}
const isAgree = ref(false)
</script>

<template>
  <uni-forms class="login-form" ref="formRef" :model="formData" :rules="formRules">
    <uni-forms-item name="mobile">
      <uni-easyinput
        :maxlength="11"
        v-model="formData.mobile"
        :input-border="false"
        :clearable="false"
        placeholder="请输入手机号"
        placeholder-style="color: #C3C3C5"
      />
    </uni-forms-item>
    <uni-forms-item name="code">
      <uni-easyinput
        :maxlength="6"
        v-model="formData.code"
        :input-border="false"
        :clearable="false"
        placeholder="请输入验证码"
        placeholder-style="color: #C3C3C5"
      />
      <text class="text-button" @tap="handleGetCode" v-if="!showCountDown">获取验证码</text>
      <view class="text-button" v-else>
        <!-- 倒计时组件 -->
        <uni-countdown
          :show-day="false"
          :show-hour="false"
          :show-minute="false"
          :day="0"
          :hour="0"
          :minute="0"
          :second="59"
          @timeup="handleTimeup"
        />
        秒后重新发送
      </view>
    </uni-forms-item>

    <view class="agreement">
      <radio :checked="isAgree" color="#16C2A3" @click="isAgree = !isAgree" />
      我已同意
      <text class="link">用户协议</text>
      及
      <text class="link">隐私协议</text>
    </view>

    <button class="uni-button" @tap="handleLogin">登 录</button>
    {{ formData }}
  </uni-forms>
</template>

<style lang="scss">
@import './styles.scss';
</style>
